package com.tangruitao.controller;


import com.tangruitao.bean.*;
import com.tangruitao.service.OrderService;
import com.tangruitao.service.impl.Dispose;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.sound.midi.Soundbank;
import javax.swing.plaf.SpinnerUI;
import java.util.ArrayList;
import java.util.List;

/**
 * 这个类的作者是13536
 * 开发日期:2021/12/30
 **/
@Controller
public class OrderController {

    @Autowired
    OrderService orderService;
    @RequestMapping("tologin")
    public String toLogin(HttpServletRequest request){
        request.setAttribute("user",new Users());
        return "loginn";
    }

    @RequestMapping("login")
    public String login(HttpSession session, @Validated @ModelAttribute("user") Users users, BindingResult result){
       if(result.hasErrors()){
           return "loginn";
       }
        Users users1 = orderService.SelectUserByName(users.getUserNumber());
       if(users1==null){
           result.rejectValue("userNumber","","工号错误");
           return "loginn";
       }
       if(users1!=null){
           if(users.getUserPassword().equals(users1.getUserPassword())){
               session.setAttribute("LOGIN_USER",users1);
               return "indext";

           }else {
               result.rejectValue("userPassword","","密码错误");
               return "loginn";
           }
       }
       return "登录";
    }

    @RequestMapping("list")
    public String orderList(HttpServletRequest request, QueryOv ov){
        Users login_user = (Users)request.getSession().getAttribute("LOGIN_USER");
        if(login_user!=null){
            if(login_user.getUserRole()==0){
                ov.setRoleId(0);
            }else{
                ov.setRoleId(login_user.getUserId());
            }
        }
        Double sum=0.0;
        List<Order> orders = orderService.selectOrder(ov);
        if(ov.getStarttime()!=null){
            for (Order order : orders) {
                String starttime = ov.getStarttime();
                String[] split = starttime.split("-");
                System.out.println("111111111111111111989"+split.length);
                String strFromDate = DateUtil.getStrFromDate(order.getOrderTime());
                String[] split1 = strFromDate.split("-");
                if(split[0].equals(split[0])){
                    System.out.println("87979887"+split[0]);
                    sum+=order.getSalary();
                }
            }
        }
        ov.setSum(sum);

        request.setAttribute("list",orders);
        request.setAttribute("ov",ov);
        return "list";
    }

    @RequestMapping("toadd")
    public String toAdd(HttpServletRequest request){
        Long sonwId = SnowUtil.getSonwId();
        request.setAttribute("l",sonwId);
        Order order = new Order();
        return "add";
    }
    @RequestMapping("add")
    public String Add(HttpServletRequest request,Order order){
        Users login_user = (Users)request.getSession().getAttribute("LOGIN_USER");
        if(login_user!=null){
            order.getUsers().setUserId(login_user.getUserId());
        }
        orderService.insertOrder(order);
        return "redirect:toadd";
    }

    @RequestMapping("bynum")
    @ResponseBody
    public int selectByNum(String num){
        Detail detail = orderService.selectDetailByNnm(num);
        if(detail!=null){
            return 1;
        }
        return 0;
    }
    @ResponseBody
    @RequestMapping("del")
    public String deleteOrder(String id){
        int i = orderService.deleteOrder(Integer.parseInt(id));
        if(i>0){
            return "redirect:list";
        }
        return "";
    }
    @RequestMapping("tree")
    @ResponseBody
    public List<Node> tree(HttpServletRequest request){
        List<Node> ll=new ArrayList<>();
        Node node = new Node();
        Users login_user = (Users)request.getSession().getAttribute("LOGIN_USER");
        if(login_user!=null){
            node.setText(login_user.getUserName());
        }
        List<Node> nodes = orderService.selectNote();
        node.setNodes(nodes);
        ll.add(node);
        return ll;
    }
    @RequestMapping("totree")
    public String totree(){
        return "indext";
    }

    @RequestMapping("shehe")
    @ResponseBody
    public String shehe(@RequestParam("name")String name, @RequestParam("time") Double time,@RequestParam("salay") Double salay){
        System.out.println("090909"+name);
        System.out.println("09090"+time);
        Requestt requestt = new Requestt(name,salay,time);
        String s = Dispose.doingYi(requestt);
        return s;
//        return "";

    }
}
